package com.example.hellofx;
import cn.hutool.extra.tokenizer.Result;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import jdbc.BuildConnection;

import java.sql.*;

public class HelloController {
    public TextField input1;
    public Label output1;
    public TextField input2;
    public Label output2;
    public TextField input3;
    public Label output3;
    public ImageView photo;


    @FXML
    protected void HelloButtonClick()
    {

        String qqNum = input1.getText();
        String sql = "SELECT name FROM qqname WHERE qq = ?";
        Connection conn = null;
        PreparedStatement p = null;
        Statement statement=new Statement() {
            @Override
            public <T> T unwrap(Class<T> iface) throws SQLException {
                return null;
            }

            @Override
            public boolean isWrapperFor(Class<?> iface) throws SQLException {
                return false;
            }

            @Override
            public ResultSet executeQuery(String sql) throws SQLException {
                return null;
            }

            @Override
            public int executeUpdate(String sql) throws SQLException {
                return 0;
            }

            @Override
            public void close() throws SQLException {

            }

            @Override
            public int getMaxFieldSize() throws SQLException {
                return 0;
            }

            @Override
            public void setMaxFieldSize(int max) throws SQLException {

            }

            @Override
            public int getMaxRows() throws SQLException {
                return 0;
            }

            @Override
            public void setMaxRows(int max) throws SQLException {

            }

            @Override
            public void setEscapeProcessing(boolean enable) throws SQLException {

            }

            @Override
            public int getQueryTimeout() throws SQLException {
                return 0;
            }

            @Override
            public void setQueryTimeout(int seconds) throws SQLException {

            }

            @Override
            public void cancel() throws SQLException {

            }

            @Override
            public SQLWarning getWarnings() throws SQLException {
                return null;
            }

            @Override
            public void clearWarnings() throws SQLException {

            }

            @Override
            public void setCursorName(String name) throws SQLException {

            }

            @Override
            public boolean execute(String sql) throws SQLException {
                return false;
            }

            @Override
            public ResultSet getResultSet() throws SQLException {
                return null;
            }

            @Override
            public int getUpdateCount() throws SQLException {
                return 0;
            }

            @Override
            public boolean getMoreResults() throws SQLException {
                return false;
            }

            @Override
            public void setFetchDirection(int direction) throws SQLException {

            }

            @Override
            public int getFetchDirection() throws SQLException {
                return 0;
            }

            @Override
            public void setFetchSize(int rows) throws SQLException {

            }

            @Override
            public int getFetchSize() throws SQLException {
                return 0;
            }

            @Override
            public int getResultSetConcurrency() throws SQLException {
                return 0;
            }

            @Override
            public int getResultSetType() throws SQLException {
                return 0;
            }

            @Override
            public void addBatch(String sql) throws SQLException {

            }

            @Override
            public void clearBatch() throws SQLException {

            }

            @Override
            public int[] executeBatch() throws SQLException {
                return new int[0];
            }

            @Override
            public Connection getConnection() throws SQLException {
                return null;
            }

            @Override
            public boolean getMoreResults(int current) throws SQLException {
                return false;
            }

            @Override
            public ResultSet getGeneratedKeys() throws SQLException {
                return null;
            }

            @Override
            public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
                return 0;
            }

            @Override
            public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
                return 0;
            }

            @Override
            public int executeUpdate(String sql, String[] columnNames) throws SQLException {
                return 0;
            }

            @Override
            public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
                return false;
            }

            @Override
            public boolean execute(String sql, int[] columnIndexes) throws SQLException {
                return false;
            }

            @Override
            public boolean execute(String sql, String[] columnNames) throws SQLException {
                return false;
            }

            @Override
            public int getResultSetHoldability() throws SQLException {
                return 0;
            }

            @Override
            public boolean isClosed() throws SQLException {
                return false;
            }

            @Override
            public void setPoolable(boolean poolable) throws SQLException {

            }

            @Override
            public boolean isPoolable() throws SQLException {
                return false;
            }

            @Override
            public void closeOnCompletion() throws SQLException {

            }

            @Override
            public boolean isCloseOnCompletion() throws SQLException {
                return false;
            }
        };
        ResultSet rs = null;
        try {
            conn = BuildConnection.getConnection();
            p = conn.prepareStatement(sql);
            p.setString(1, qqNum);
            rs = p.executeQuery();
            if (rs.next()) {
                output1.setText("该用户QQ名为：" + rs.getString("name"));
            }
            else{
                String qqNumber= input1.getText();
                String R;
                String url1="https://qqlykm.cn/api/qq/api.php?qq="+qqNumber;
                R = HttpUtil.get("https://qqlykm.cn/api/qq/api.php?qq="+qqNumber);
                String result1=HttpUtil.get(url1);
                JsonRootBean j1=JSONUtil.toBean(result1,JsonRootBean.class);
                j1.getName();
                R="qq号为"+qqNumber;
                R+="\n网名为:"+j1.getName();
                R+="\n头像为:";
                Image image1=new Image(j1.getImgurl());
                photo.setImage(image1);
                output1.setText(R);
                String sql1 ="insert into qqname value('"+qqNumber+"','"+j1.getName()+"')";
                statement.executeUpdate(sql1);
                System.out.println("该qq信息不存在，已往数据库添加数据");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (p != null)
                    p.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    @FXML
    protected void HelloButtonClick1()
    {
        String Number = input2.getText();
        String sql = "SELECT YorN FROM srnumber WHERE number = ?";
        Connection conn = null;
        PreparedStatement q = null;
        ResultSet rs = null;
        try {
            conn = BuildConnection.getConnection();
            q = conn.prepareStatement(sql);
            q.setString(1, Number);
            rs = q.executeQuery();
            if (rs.next()) {
                output2.setText("是否为诈骗电话电话：" + rs.getString("YorN"));
            }
            else{
                String phoneNum=input2.getText();
                String Result=HttpUtil.get("https://qqlykm.cn/api/phone/phone.php?phone="+phoneNum);
                Result = JSONUtil.toJsonPrettyStr(Result);
                output2.setText(Result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (q != null)
                    q.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
    @FXML
    protected void HelloButtonClick2() {
        String weathers = input3.getText();
        String sql = "SELECT date,tem,fengxiang,fengli,type,city FROM weather WHERE city = ?";
        Connection conn = null;
        PreparedStatement p = null;
        ResultSet rs = null;
        try {
            conn = BuildConnection.getConnection();
            p = conn.prepareStatement(sql);
            p.setString(1, weathers);
            rs = p.executeQuery();
            if (rs.next()) {
                output3.setText("日期：" + rs.getString("date") + ",温度：" + rs.getString("tem") + ",风向："
                        + rs.getString("fengxiang") + ",风力：" + rs.getString("fengli") + ",类型：" + rs.getString
                        ("type") + ",城市：" + rs.getString("city"));

            } else {
                String baseUrl1 = "https://qqlykm.cn/api/api/tq.php?city=";
                String Id1 = input3.getText();
                String Id1Result = HttpUtil.get(baseUrl1 + Id1);
                Id1Result = JSONUtil.toJsonPrettyStr(Id1Result);
                output3.setText(Id1Result);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null)
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                if (p != null)
                    p.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
